package com.scst.controller.admin;

import com.scst.constant.JwtClaimsConstant;
import com.scst.dto.TeacherDTO;
import com.scst.dto.TeacherLoginDTO;
import com.scst.entity.Teacher;
import com.scst.properties.JwtProperties;
import com.scst.result.Result;
import com.scst.service.TeacherService;
import com.scst.utils.JwtUtil;
import com.scst.vo.TeacherLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.AccountNotFoundException;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Api("老师接口")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    @ApiOperation("老师登录")
    public Result<TeacherLoginVO> login(@RequestBody TeacherLoginDTO teacherLoginDTO) throws AccountNotFoundException {
        log.info("老师登录：{}", teacherLoginDTO);

        Teacher teacher = teacherService.login(teacherLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, teacher.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        TeacherLoginVO teacherLoginVO=TeacherLoginVO.builder()
                .id(teacher.getId())
                .name(teacher.getName())
                .className(teacher.getClassName())
                .token(token)
                .build();

        return Result.success(teacherLoginVO);
    }
    @PostMapping
    public Result<String> save(@RequestBody TeacherDTO teacherDTO) {
        return Result.success();
    }

 }
